<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

/**
 * 示例接口
 */
class Message extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    // 获取access_token
    public function getAccessToken()
    {

        $appId='wx06b2a559d1df9a9c';
        $appSecret= '3232307ce5f27b99beec2b81b4468f95';

        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $res = file_get_contents($url);
        $res = json_decode($res, true);


        return $res['access_token'];
    }


    //记一笔通知  ???111
    public function sendmsg(){


        $mobile=$this->request->param('mobile');
        $user=Db::name('user')->where(['unionid'=>$mobile])->find();


        $one=$this->request->param('one');
        $sku=$this->request->param('sku');
        $times=$this->request->param('times');
        $id=$this->request->param('id');



        // 获取access_token
        $accessToken = $this->getAccessToken();
        // 定义推送消息的接口地址
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

// 工单名称   服务器网络问题处理
//项目名称 万科.金色领域
//发起人 姓名
//到期时间  2020-09-15 18:12:30


        $data = [
            "touser" => $user['openid'],
            "template_id" =>"lo2784sTLyDGLfqPFtrdbicmNYw8lrJTXldRuvyRBI4",
            "miniprogram"=>['appid'=>'wxc727205a0a40319a','pagepath'=>'/pages/note/info?id='.$id],
            "data" =>[
                'thing1'=>['value'=>'新的记一笔','color'=>''],
                'thing4'=>  ['value'=>$sku,'color'=>''],
                'thing8'=>  ['value'=>$one,'color'=>''],
                'time9'=>  ['value'=>$times,'color'=>'']
                ]
            ];



    // 将推送消息转换为JSON格式
    $dataJson = json_encode($data);

    // 发送推送消息
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $result = curl_exec($ch);
    curl_close($ch);

    $result = json_decode($result, true);
//    if ($result["errcode"] == 0) {
//        return "推送成功！";
//    } else {
//        return "推送失败，错误代码：" . $result["errcode"] . "，错误信息：" . $result["errmsg"];
//    }



    }

    //记一笔通知  ???111
    public function sendmsgtouser(){


        $mobile=$this->request->param('mobile');
        $user=Db::name('user')->where(['unionid'=>$mobile])->find();


        $one=$this->request->param('one');
        $sku=$this->request->param('sku');
        $times=$this->request->param('times');
        $id=$this->request->param('id');



        // 获取access_token
        $accessToken = $this->getAccessToken();
        // 定义推送消息的接口地址
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

        // 工单名称   服务器网络问题处理
        //项目名称 万科.金色领域
        //发起人 姓名
        //到期时间  2020-09-15 18:12:30

var_dump($accessToken);

        $data = [
            "touser" => $user['openid'],
            "template_id" =>"lo2784sTLyDGLfqPFtrdbicmNYw8lrJTXldRuvyRBI4",
            "miniprogram"=>['appid'=>'wx5b4d00257364ffad','pagepath'=>'/pages/note/info?id='.$id],
            "data" =>[
                'thing1'=>['value'=>'新的记一笔','color'=>''],
                'thing4'=>  ['value'=>$sku,'color'=>''],
                'thing8'=>  ['value'=>$one,'color'=>''],
                'time9'=>  ['value'=>$times,'color'=>'']
            ]
        ];



        // 将推送消息转换为JSON格式
        $dataJson = json_encode($data);

        // 发送推送消息
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($result, true);
    if ($result["errcode"] == 0) {
        return "推送成功！";
    } else {
        return "推送失败，错误代码：" . $result["errcode"] . "，错误信息：" . $result["errmsg"];
    }



    }




    //记一笔通知  ???111
    public function sendmsgfroaddcase(){


        $mobile=$this->request->param('mobile');
        $user=Db::name('user')->where(['unionid'=>$mobile])->find();


        $one=$this->request->param('one');
        $sku=$this->request->param('sku');
        $times=$this->request->param('times');
        $id=$this->request->param('id');



        // 获取access_token
        $accessToken = $this->getAccessToken();
        // 定义推送消息的接口地址
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

// 工单名称   服务器网络问题处理
//项目名称 万科.金色领域
//发起人 姓名
//到期时间  2020-09-15 18:12:30


        $data = [
            "touser" => $user['openid'],
            "template_id" =>"lo2784sTLyDGLfqPFtrdbicmNYw8lrJTXldRuvyRBI4",
            "miniprogram"=>['appid'=>'wxc727205a0a40319a','pagepath'=>'/pages/project/info?id='.$id],
            "data" =>[
                'thing1'=>['value'=>'项目已经创建 ','color'=>''],
                'thing4'=>  ['value'=>$sku,'color'=>''],
                'thing8'=>  ['value'=>$one,'color'=>''],
                'time9'=>  ['value'=>$times,'color'=>'']
            ]
        ];



        // 将推送消息转换为JSON格式
        $dataJson = json_encode($data);

        // 发送推送消息
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($result, true);
//    if ($result["errcode"] == 0) {
//        return "推送成功！";
//    } else {
//        return "推送失败，错误代码：" . $result["errcode"] . "，错误信息：" . $result["errmsg"];
//    }



    }



    //记一笔通知  ???111
    public function sendmsgfroaddcasetouser(){


        $mobile=$this->request->param('mobile');
        $user=Db::name('user')->where(['unionid'=>$mobile])->find();


        $one=$this->request->param('one');
        $sku=$this->request->param('sku');
        $times=$this->request->param('times');
        $id=$this->request->param('id');


        $accessToken = $this->getAccessToken();

      // 定义推送消息的接口地址
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

// 工单名称   服务器网络问题处理
//项目名称 万科.金色领域
//发起人 姓名
//到期时间  2020-09-15 18:12:30


        $data = [
            "touser" => $user['openid'],
            "template_id" =>"lo2784sTLyDGLfqPFtrdbicmNYw8lrJTXldRuvyRBI4",
            "miniprogram"=>['appid'=>'wxc727205a0a40319a','pagepath'=>'/pages/project/info?id='.$id],
            "data" =>[
                'thing1'=>['value'=>'项目已经创建 ','color'=>''],
                'thing4'=>  ['value'=>$sku,'color'=>''],
                'thing8'=>  ['value'=>$one,'color'=>''],
                'time9'=>  ['value'=>$times,'color'=>'']
            ]
        ];



        // 将推送消息转换为JSON格式
        $dataJson = json_encode($data);

        // 发送推送消息
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataJson);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        $result = curl_exec($ch);
        curl_close($ch);

        $result = json_decode($result, true);
//    if ($result["errcode"] == 0) {
//        return "推送成功！";
//    } else {
//        return "推送失败，错误代码：" . $result["errcode"] . "，错误信息：" . $result["errmsg"];
//    }



    }






}